package org.yxjme.exjni_two.a;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.jbh.permissionlibrary.PermissionManager;
import com.jbh.permissionlibrary.i.CheckPermissionListener;

import org.yxjme.exjni_two.ExJNI;
import org.yxjme.exjni_two.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private ExJNI exJNI =  new ExJNI();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.sample_text);
        PatternTask(regexPhone,inout2);
    }



    private void PatternTask(String regex,String in) {
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(in);
        boolean b = m.find();
        Log.v("測試","m = "+b) ;
        if (b){
            Log.v("測試","m = "+m.group(0)) ;
        }
    }


    String regex = "(^\\d{2})(zo*)(\\d+)?";
    String inout1 = "10zo20010";


    String regexPhone = "(15[0-9]|13[0-9]|16[0-9]|17[0-9]|18[0-9])\\d{8}(\\s+)?";
    String inout2 = "1630000000025";

    /**
     * 录制视频
     *
     * @param view
     */
    public void RecordVideo(View view) {
        List<String> list = new ArrayList<>();
        list.add(Manifest.permission.CAMERA);
        list.add(Manifest.permission.RECORD_AUDIO);
        list.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        PermissionManager.newInstance().checkPermission(MainActivity.this, list, new CheckPermissionListener() {
            /**
             * 不需要授权
             */
            @Override
            public void onNoNeedCheckPermission() {
                /*使用jni调用Java实现跳转*/
                exJNI.recordVideo(MainActivity.this);
                /*使用反射*/
//                toRecord();
            }
        });
    }


    /**
     * 使用反射实现跳转
     */
    private void toRecord() {
        Class<? extends ExJNI> cls = exJNI.getClass();
        try {

            ExJNI obj = cls.newInstance();
            /*获取需要反射的方法*/
            Method m = cls.getDeclaredMethod(
                    "toActivity", Context.class, Class.class);
            /*反射私有方法必须添加此方法才可调用*/
            m.setAccessible(true);
            /*开始调用该方法*/
            m.invoke(exJNI, MainActivity.this, RecordVideoActivity.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }


    /**
     * 拍照
     *
     * @param view
     */
    public void takePhoto(View view) {
        List<String> list = new ArrayList<>();
        list.add(Manifest.permission.CAMERA);
        list.add(Manifest.permission.RECORD_AUDIO);
        list.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        PermissionManager.newInstance().checkPermission(MainActivity.this, list, new CheckPermissionListener() {
            /**
             * 不需要授权
             */
            @Override
            public void onNoNeedCheckPermission() {
                exJNI.takePhoto(MainActivity.this);
            }
        });
    }


    /**
     * jni功能
     *
     * @param view
     */
    public void CeShiJNI(View view) {
        tv.setText(exJNI.getMessage());
        exJNI.GetOBJ("测试哦");
        int[] ids ={
                00,11,22,10
        };
        exJNI.arrayLength(ids);
        exJNI.createJNIThread();
    }
}
